#pragma once
#include "stdafx.h"
#include "Application.h"
#include "OgreFramework.h"
#include "InputManager.h"
#include "InputSubscriber.h"
#include "MonkeyScene.h"


#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>

namespace Test
  {
  class DefaultInputListener : public InputSubscriber
    {
    private:
      Application& m_application;
    public:
      DefaultInputListener(Application& i_application)
        : m_application(i_application)
        {        }

      virtual bool KeyReleased(const OIS::KeyEvent &keyEventRef) override
        {
        if (keyEventRef.key == OIS::KC_ESCAPE)
          {
          m_application.Shutdown();
          return true;
          }
        return false;
        }
    };
  } // namespace


void Application::Start()
	{
	m_shutdown = false;

	mp_ogre.reset(new OgreFramework());
	mp_ogre->Initialize();

	
	mp_input_manager.reset(new InputManager(*mp_ogre));
	mp_input_manager->AddSubscriber(std::make_shared<Test::DefaultInputListener>(*this));
	
	mp_monkey_scene = std::make_shared<MonkeyScene>(*mp_ogre);
  mp_input_manager->AddSubscriber(mp_monkey_scene);

	mp_ogre->GetRoot()->addFrameListener(this);
	mp_ogre->GetRoot()->startRendering();
	
	}
void Application::Shutdown()
	{
	m_shutdown = true;
	}

bool Application::frameRenderingQueued(const Ogre::FrameEvent& evt)
  {
  if(m_shutdown)
    return false;
  float sleepTime = 0.018 - evt.timeSinceLastFrame;
  sleepTime *= 1000.0f;
  if(0 < sleepTime)
    Sleep(sleepTime);

	if(mp_ogre->GetKeyboard()->isKeyDown(OIS::KC_ESCAPE))
		return false;
	mp_input_manager->Update(evt.timeSinceLastFrame);
  //mp_monkey_scene->CameraMan().frameRenderingQueued(evt);

  //mp_input_manager->Update(evt.timeSinceLastFrame);
  return true;
  }